#include <stdio.h>
#include <stdlib.h>
#include "util.h"
#include "absyn.h"
#include "grammer.tab.h"
#include "scan.h"

extern A_exp absyn_root;	// Root node of the abstract tree
extern FILE *yyin;
extern char *yytext;
extern void yyparse();

extern void EM_reset(char*);
extern void scan(A_exp, int);	// scan and destroy the abstract tree
extern void destroy(A_exp);

string toknames[] = 
{
"ID", "STRING", "INT", "COMMA", "COLON", "SEMICOLON", "LPAREN",
"RPAREN", "LBRACK", "RBRACK", "LBRACE", "RBRACE", "DOT", "PLUS",
"MINUS", "TIMES", "DIVIDE", "EQ", "NEQ", "LT", "LE", "GT", "GE",
"AND", "OR", "ASSIGN", "ARRAY", "IF", "THEN", "ELSE", "WHILE", "FOR",
"TO", "DO", "LET", "IN", "END", "OF", "BREAK", "NIL", "FUNCTION",
"VAR", "TYPE"
};

string tokname(tok) 
{
	return tok<258 || tok>300 ? "BAD_TOKEN" : toknames[tok-258];
};

int main(int argc, char **argv)
{
	EM_reset("test.bm");
	if (!yyin)
	{
		fprintf(stderr,"Can't open file!");
		exit(1);
	}

	yyparse();
	if (absyn_root!=NULL)
	{
		scanTree(absyn_root, 0);
	}
	else
		printf("Error!");

	printf("\n");
	getchar();

	return 0;
}
